<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2019/2/23
 * Time: 10:53 AM
 */

namespace backend\models;


use common\helpers\StringHelper;

class SearchPart extends \common\models\JobType
{
    public function rules()
    {
        return [
            [['name', 'type'], 'required'],
            [['type'], 'integer'],
            [['name'], 'string', 'max' => 255],
            ['value', 'validateValue'],
        ];
    }


    public function beforeValidate()
    {
        if ($this->type == self::TYPE_TIME) {
            $this->value = strtotime($this->value);
        }

        return parent::beforeValidate(); // TODO: Change the autogenerated stub
    }

    public function validateValue()
    {
        if ( ! $this->hasErrors()) {
            switch ($this->type) {
                case self::TYPE_TIME:
                    if ( ! preg_match("/^[0-9]*$/", $this->value)) {
                        $this->addError('value', '请输入正确的时间');
                    }
                    break;
                case self::TYPE_LOCATION:
                    if ( ! preg_match("/^\d{6}$/", $this->value)) {
                        $this->addError('value', '6位区划码不正确，请确认后输入!');
                    }
                    break;
                case self::TYPE_SALARY:
                    if ( ! preg_match("/^[0-9]*$/", $this->value)) {
                        $this->addError('value', '不是整数！');
                    }
                    break;
            }
        }
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id'    => 'ID',
            'name'  => '名称',
            'type'  => '类型',
            'value' => '值',
        ];
    }


    public function beforeSave($insert)
    {
        switch ($this->type) {
            case self::TYPE_TIME:
                break;
            case self::TYPE_LOCATION:
                break;
            case self::TYPE_SALARY:
                break;
        }

        return parent::beforeSave($insert);
    }


}